package com.hoo.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import com.hoo.entity.User.User;
import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;
import com.sap.mw.jco.JCO.FieldIterator;

/**
 * * <b>function:</b> WebService传递复杂对象，如JavaBean、Array、List、Map等 *
 */
@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class ComplexUserService implements IComplexUserService {

	/** 读库存 */
	private static final String SCM_MB52 = "ZIF_OMS_MAT_STOCK_GET";

	public JCO.Client mConnection;
	public JCO.Repository mRepository;

	@Resource(name = "sapURL")
	public String sapURL;

	@Resource(name = "sapUserid")
	public String sapUserid;

	@Resource(name = "sapPwd")
	public String sapPwd;

	@Resource(name = "language")
	public String language;

	@Resource(name = "sapClientPort")
	public String sapClientPort;

	public User getUserByName(@WebParam(name = "name") String name) {
		User user = new User();
		user.setId(new Date().getSeconds());
		user.setName(name);
		user.setAddress("china");
		user.setEmail(name + "@gqb.com");
		return user;
	}

	@Override
	public void setUser(User user) {

		System.out.println("############Server setUser###########");
		System.out.println("setUser:" + user);

	}

	// 根据提供的函数名称创建一个函数
	public JCO.Function createFunction(String name) throws Exception {
		// try {
		// 对象包含着某个特定的RFM元数据
		IFunctionTemplate ft = null;
		if (name != null) {
			if (mRepository == null) {
				throw new Exception("mRepository为空");
			}
			ft = mRepository.getFunctionTemplate(name.toUpperCase());

			if (ft == null) {
				throw new Exception("ft为空");
			}
		} else {
			throw new Exception("ft为空");
		}

		return ft.getFunction();

	}

	/**
	 * 读物料列表
	 * */
	@Override
	public List<HashMap> getItemInventory(String ip_matnr, String iv_vbeln,
			String zuzhi, String kuwei) throws Exception {

		List<HashMap> result = new ArrayList<HashMap>();
		JCO.Function function = null;
		JCO.Table ztmtrack = null;
		try {

			getConnection();
			function = createFunction(SCM_MB52); // 取物料数据表
			if (function == null) {
				System.out.println(SCM_MB52 + "在sap中没有发现");
			} else {
				/**
				 *输入要读取物料号码
				 * */
				JCO.ParameterList input = function.getImportParameterList();
				input.setValue(ip_matnr, "IV_MATNR");// IP_MATNR IV_MATNR物料号码
				input.setValue(iv_vbeln, "IV_VBELN");// I销售单号

				JCO.ParameterList list = function.getTableParameterList();// BT是按段，EQ是相等，CP是模糊三种匹配值
				/**
				 * 输入指定的工厂地点
				 * 
				 * */
				JCO.Table it_werks = list.getTable("IT_WERKS");
				it_werks.appendRow();
				it_werks.setRow(0);
				it_werks.setValue("I", "SIGN");
				it_werks.setValue("EQ", "OPTION");
				it_werks.setValue(zuzhi, "LOW");
				it_werks.setValue("", "HIGH");

				/**
				 * 输入指定的库存地点
				 * */
				JCO.Table it_lgort = list.getTable("IT_LGORT");
				it_lgort.appendRow();
				it_lgort.setRow(0);
				it_lgort.setValue("I", "SIGN");
				it_lgort.setValue("EQ", "OPTION");
				it_lgort.setValue(kuwei, "LOW");//
				it_lgort.setValue("", "HIGH");// 11103015000009

				if (mConnection == null) {
					System.out.println("mConnection 为空");
				}
				mConnection.execute(function);

				ztmtrack = list.getTable("ET_OUTPUT"); // 是一个内表。
				result = table2List(ztmtrack);

			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw ex;

		} finally {
			if (mConnection != null) {
				mConnection.disconnect();
			}
		}
		return result;
	}

	private List table2List(JCO.Table table) {
		List<HashMap> result = new ArrayList<HashMap>();
		if (table.isEmpty())
			return result;

		for (int i = 0; i < table.getNumRows(); i++) {
			table.setRow(i);
			HashMap record = new HashMap();
			FieldIterator fields = table.fields();
			while (fields.hasMoreElements()) {
				String field = (String) fields.nextElement();
				record.put(field, table.getValue(field));
			}
			result.add(record);
		}
		return result;
	}

	private void getConnection() throws Exception {

		try {
			mConnection = JCO.createClient(sapClientPort, // SAP client
					sapUserid, // userid
					sapPwd, // password
					language, // language
					sapURL, // application server host name
					"00"); // system number
			mConnection.connect();
			mRepository = new JCO.Repository("ARAsoft", mConnection);
			System.out.println("SAP连接成功");

		} catch (Exception ex) {
			System.out.println("SAP连接不成功");
			throw new Exception("JCO.Repository对象生成不成功");
		}

	}

}
